package cao.oschina.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.material.widget.ActionView;
import android.material.widget.NavigationView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cao.oschina.R;
import hd.util.ScreenHelper;


/**
 * ToolBarView
 *
 * @see ViewGroup
 * <p/>
 * Created by Haidy on 15/1/4.
 */
public class ToolBarView extends lib.toolbar.widget.ToolBarView {

    private static final int ID_NAV = 0x1000;
    private static final int ID_AC = 0x1001;
    private NavigationView navigationView;
    private RelativeLayout actionView;
    private TextView titleView;
    private int backgroundColor = Color.GREEN;

    public ToolBarView(Context context) {
        this(context, null);
    }

    public ToolBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToolBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        addViews();
    }

    /**
     * 添加视图
     */
    private void addViews() {
        //添加导航按钮
        navigationView = new NavigationView(getContext());
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        navigationView.setLayoutParams(lp);
        navigationView.setId(ID_NAV);

        //添加Action,Action放在一LinearLayout
        actionView = new RelativeLayout(getContext());
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        lp.addRule(ALIGN_PARENT_RIGHT);
        actionView.setLayoutParams(lp);
        actionView.setId(ID_AC);

        //添加标题
        titleView = new TextView(getContext());
        titleView.setSingleLine();
        titleView.setTextColor(Color.WHITE);
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
        titleView.setGravity(CENTER_VERTICAL);
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_VERTICAL);
        lp.addRule(RelativeLayout.RIGHT_OF, ID_NAV);
        lp.addRule(RelativeLayout.LEFT_OF, ID_AC);
        titleView.setLayoutParams(lp);

        addView(navigationView);
        addView(actionView);
        addView(titleView);
    }

    /**
     * 获取导航View
     *
     * @return nav_view
     */
    public NavigationView getNavigationView() {
        return navigationView;
    }

    /**
     * 获取ActionView
     *
     * @return actionView
     */
    public RelativeLayout getActionView() {
        return actionView;
    }

    public void addAction(String actionName, Drawable icon, int id) {
        ActionView v = new ActionView(getContext());
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        int count = actionView.getChildCount();
        int height = getResources().getDimensionPixelSize(R.dimen.titleBarHeight);
        lp.leftMargin = height * count;
        v.setLayoutParams(lp);
        v.setBackgroundColor(backgroundColor);
        if (icon != null) {
            int size = ScreenHelper.dp2px(getContext(), 24);
            icon.setBounds(0, 0, size, size);
            v.setActionIcon(icon);
        } else {
            v.setActionName(actionName);
        }
        v.setId(id);
        actionView.addView(v);
    }

    /**
     * 添加Action
     *
     * @param type 类型
     * @param id   id
     */
    public void addAction(int type, int id) {
        ActionView action = new ActionView(getContext());
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        int count = actionView.getChildCount();
        int height = getResources().getDimensionPixelSize(R.dimen.titleBarHeight);
        lp.leftMargin = height * count;
        action.setLayoutParams(lp);
        action.setBackgroundColor(backgroundColor);
        action.setType(type);
        action.setId(id);
        actionView.addView(action);
    }

    /**
     * 设置监听
     *
     * @param listener listener
     */
    public void setOnActionListener(OnClickListener listener) {
        int count = actionView.getChildCount();
        for (int i = 0; i < count; i++) {
            actionView.getChildAt(i).setOnClickListener(listener);
        }
    }

    /**
     * 设置标题
     *
     * @param title title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }

    /**
     * 设置标题
     *
     * @param string title
     */
    public void setTitle(int string) {
        titleView.setText(string);
    }

    /**
     * 设置标题
     *
     * @param title title
     */
    public void setTitle(CharSequence title) {
        titleView.setText(title);
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
        this.backgroundColor = color;
        navigationView.setBackgroundColor(color);
    }

}
